package com.magicsoft.share.ui.share;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.adapter.share.SearchFragmentAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.view.share.ISearchView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_search)
    EditText tvSearch;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPage)
    ViewPager viewPage;


    private SearchFragmentAdapter searchFragmentAdapter;
    private ArrayList<Fragment> fragments;
    private SearchFragment searchArticleFragment;
    private SearchFragment searchUserFragment;
    private ArrayList<String> titles;

    @Override
    public BasePresenter<ISearchView> initPresenter() {
        return null;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    public void initData() {
        searchUserFragment = new SearchFragment();
        Bundle bundle = new Bundle();
        bundle.putString("searchType", "用户");

        searchUserFragment.setArguments(bundle);
        searchArticleFragment = new SearchFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putString("searchType", "文章");

        searchArticleFragment.setArguments(bundle1);
        fragments = new ArrayList<>();
        fragments.add(searchUserFragment);
        fragments.add(searchArticleFragment);
        titles = new ArrayList<>();
        titles.add("用户");
        titles.add("文章");

    }

    @Override
    public void initView() {

        searchFragmentAdapter = new SearchFragmentAdapter(getSupportFragmentManager());
        searchFragmentAdapter.refreshData(fragments, titles);
        viewPage.setAdapter(searchFragmentAdapter);
        tabLayout.setupWithViewPager(viewPage);

        tvSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String keyword = tvSearch.getText().toString();

                    searchUserFragment.requestDatas(keyword);

                    searchArticleFragment.requestDatas(keyword);


                }
                return false;
            }
        });


    }

    @Override
    public void requestData() {

    }


    @OnClick(R.id.tv_title_right)
    public void onViewClicked() {
        finish();
    }


}
